package com.xc.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.content.mapper.TeachplanMapper;
import com.xc.content.mapper.TeachplanMediaMapper;
import com.xc.content.model.dto.SaveTeachplanDto;
import com.xc.content.model.dto.TeachplanDto;
import com.xc.content.model.po.Teachplan;
import com.xc.content.model.po.TeachplanMedia;
import com.xc.content.service.TeachplanService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author daxiaa
 */
@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> queryTeachPlanByCourseId(Long courseId) {
        List<TeachplanDto> list = teachplanMapper.queryTeachPlanTreeNode(courseId);
        return list;
    }

    @Transactional
    @Override
    public void saveOrUpdateTeachplan(SaveTeachplanDto saveTeachplanDto) {
        if (saveTeachplanDto.getId() != null) {
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto, teachplan);
            teachplan.setChangeDate(LocalDateTime.now());
            //id不为空的时候，直接更新
            saveOrUpdate(teachplan);
        } else {
            //新增的时候，需要考虑这个章、节 同一个父亲 下的排序
            LambdaQueryWrapper<Teachplan> lambdaQueryWrapper = new LambdaQueryWrapper();
            //父亲id可能都是0 所以 还需要通过课程ID 这个条件
            lambdaQueryWrapper.eq(Teachplan::getCourseId, saveTeachplanDto.getCourseId());
            lambdaQueryWrapper.eq(Teachplan::getParentid, saveTeachplanDto.getParentid());
            //查询到的数量
            Long count = teachplanMapper.selectCount(lambdaQueryWrapper);
            if (count == null) {
                count = 1L;
            } else {
                //排序+1
                count += 1;
            }
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto, teachplan);
            teachplan.setOrderby(count.intValue());
            teachplan.setChangeDate(LocalDateTime.now());
            save(teachplan);
        }


    }

    @Transactional
    @Override
    public void delTeachplan(Long id) {
       //把底下的孩子先删除
        LambdaQueryWrapper<Teachplan> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Teachplan::getParentid,id);
        remove(lambdaQueryWrapper);
        //再删除自己
        removeById(id);
        //删除该课程计划下的关联媒体记录
        LambdaQueryWrapper<TeachplanMedia> lambdaQueryWrapper2 = new LambdaQueryWrapper();
        lambdaQueryWrapper2.eq(TeachplanMedia::getTeachplanId,id);
        teachplanMediaMapper.delete(lambdaQueryWrapper2);
    }

    @Override
    public void moveup(Long id) {
        //获取自己的排序
        Teachplan teachplan = getById(id);
        Integer orderby = teachplan.getOrderby();
       //获取上面邻居的排序
        LambdaQueryWrapper<Teachplan> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Teachplan::getOrderby,orderby-1);
        lambdaQueryWrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        lambdaQueryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId());
        Teachplan teachplanUp =  teachplanMapper.selectOne(lambdaQueryWrapper);
        if(teachplanUp!=null){
            teachplan.setOrderby(orderby-1);
            teachplanUp.setOrderby(orderby);
        }
        saveOrUpdate(teachplan);
        saveOrUpdate(teachplanUp);

    }

    @Override
    public void movedown(Long id) {
        //获取自己的排序
        Teachplan teachplan = getById(id);
        Integer orderby = teachplan.getOrderby();
        //获取上面邻居的排序
        LambdaQueryWrapper<Teachplan> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Teachplan::getOrderby,orderby+1);
        lambdaQueryWrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        lambdaQueryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId());
        Teachplan teachplanDown =  teachplanMapper.selectOne(lambdaQueryWrapper);
        if(teachplanDown!=null){
            teachplan.setOrderby(orderby+1);
            teachplanDown.setOrderby(orderby);
        }
        saveOrUpdate(teachplan);
        saveOrUpdate(teachplanDown);

    }
}
